package com.ws.shop.action.front;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.action.admin.AccessoryAction;
import com.ws.shop.bean.Page;
import com.ws.shop.entity.Accessory;
import com.ws.shop.entity.Goods;
import com.ws.shop.entity.Mall;
import com.ws.shop.entity.Member;
import com.ws.shop.entity.Order;
import com.ws.shop.service.GoodsService;
import com.ws.shop.service.MallService;
import com.ws.shop.util.CommonUtil;
import com.ws.shop.util.RequestUtil;
import com.ws.shop.util.StringUtil;


@Controller
@RequestMapping(value = "/mall/*")
public class MallAction  extends BaseFrontAction {

	@Autowired
	private MallService mallService;
	@Autowired
	private GoodsService goodsService;
	
	@RequestMapping(value = "index.htm", method = RequestMethod.GET)
    public String index(HttpServletRequest request,Model model){
		int cid = RequestUtil.getIntParameter(request, "cid", 0);
		int state = RequestUtil.getIntParameter(request, "state", 0);
		String score = RequestUtil.getStringParameter(request, "score", "0;0");
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		String[] scores = score.split(";");
		int startScore = 0,endScore = 0;
		try{
			startScore = Integer.parseInt(scores[0]);
			endScore = Integer.parseInt(scores[1]);
			if(startScore >= endScore)
				endScore = 0;
		}catch(Exception e){
			startScore = 0;endScore = 0;
		}
		Page page = mallService.findMallPage(pageno, 20, cid,startScore,endScore,state);
		model.addAttribute(Page.PAGE, page);
		model.addAttribute("cid", cid);
		model.addAttribute("state", state);
		model.addAttribute("score", startScore+";"+endScore);
		model.addAttribute("saveImagUrl", AccessoryAction.SAVE_IMAG_URL);
    	return "mall_index";
    }
    
    //详情
//	@RequestMapping(value = "detail.htm", method = RequestMethod.GET)
//	public String detail(HttpServletRequest request,Model model){
//		int id = RequestUtil.getIntParameter(request, "id", 0); //积分 商城的ID
//		Mall mall = mallService.findMallById(id);
//		Goods goods = goodsService.findGoodsById(mall.getGid());
//		List<Accessory> accessoryList = goodsService.findAccessoryListByGoodsId(mall.getGid());
//		model.addAttribute("mall", mall);
//		model.addAttribute("goods", goods);
//		model.addAttribute("accessoryList", accessoryList);
//    	model.addAttribute("saveImagUrl", AccessoryAction.SAVE_IMAG_URL);
//		return "mall_detail";
//	}    
    
    //	购买
//	@RequestMapping(value = "buy.htm", method = RequestMethod.GET)
//    public String buy(HttpServletRequest request,Model model){
//		int id = RequestUtil.getIntParameter(request, "id", 0); //积分 商城的ID
//		Map<String,Object> loginMember = getLoginMember(request.getSession(false));
//    	if(loginMember==null){
//    		return ERROR;
//    	}
//		Mall mall =  mallService.findMallById(id);
//		Goods goods = goodsService.findGoodsById(mall.getGid());
//	
//		if (mall==null){
//		    //操作错误
//		    return ERROR;
//		}
//		//$sconfig=C('SYSCONFIG');
//		model.addAttribute("mall", mall);
//		request.setAttribute("goods", goods);
//    	return "buy";
//    }
    
	//检查支付,兑换商品
	@RequestMapping(value = "check.htm", method = RequestMethod.GET)
    public String check(HttpServletRequest request,HttpServletResponse response){
		try{
		int id = RequestUtil.getIntParameter(request, "id", 0); //积分 商城的ID
		int num = RequestUtil.getIntParameter(request, "num", 0);
		String recipient = RequestUtil.getStringParameter(request, "recipient", null);//收件人
		String postalcode = RequestUtil.getStringParameter(request, "postalcode", null); //邮编
		String address = RequestUtil.getStringParameter(request, "address", null);
		String tel = RequestUtil.getStringParameter(request, "tel", null);
		String remark = RequestUtil.getStringParameter(request, "remark", null);
    
    	Map<String,Object> loginMember = getLoginMember(request.getSession(false));
		if (loginMember == null){
			return this.ajaxJsonErrorMessage(response, "请登陆!");
		}
		if (num<=0){
		    return this.ajaxJsonErrorMessage(response, "数量不能为空!");
		}
		if (StringUtil.isEmpty(recipient)){
		    return this.ajaxJsonErrorMessage(response, "收件人不能为空!");
		}
		if (StringUtil.isEmpty(postalcode)){
		    return this.ajaxJsonErrorMessage(response, "邮编不能为空!");
		}
		if (StringUtil.isEmpty(address)){
		    return this.ajaxJsonErrorMessage(response, "收货地址不能为空!");
		}
		if (StringUtil.isEmpty(tel)){
		    return this.ajaxJsonErrorMessage(response, "收件人手机号不能为空!");
		}
		
	    int u_id = (Integer)loginMember.get(Member.LOGIN_MEMBER_ID_KEY);
	    String u_name = (String)loginMember.get(Member.LOGIN_MEMBER_NAME_KEY);

		Order order = new Order();
		order.setUid(u_id);
		order.setOrdersn(CommonUtil.getUUID());
		order.setNum(num);
		order.setAid(id);
		order.setName(u_name);
		order.setRecipient(recipient);
		order.setAddress(address);
		order.setPostalcode(postalcode);
		order.setTel(tel);
		order.setRemark(remark);
		order.setType(2);
		order.setState(1);
        Object[] result = mallService.integralConversion(order);
	    if((Integer)result[0]==0){
			this.ajaxJsonSuccessMessage(response, "兑换商品成功!");
		}else{
			 return this.ajaxJsonErrorMessage(response, (String)result[1]);
		}
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return this.ajaxJsonErrorMessage(response, "系统忙!");
    }
    
}
